package com.tul.exercise.base.java8.interfaceDemo;

import com.tul.exercise.base.java8.stream.base.Apple;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;

/**
 * Consumer<T>使用示例
 *
 * @author tuliang
 */
public class ConsumerTest {
    @Test
    @DisplayName("Consumer<T>演示")
    public void test01() {
        List<Apple> apples = Apple.createApples(5);

        // 打印Apple信息
        Consumer<Apple> printAppleInfo = (Apple a) -> print(a);
        apples.stream().forEach(printAppleInfo);

        System.out.println("\n");

        // 计算并打印价格
        Double price = 23.9;
        Consumer<Apple> calAndPrinPrice = (Apple a) ->printPrice(a,price);
        System.out.println("Price($)\tTotal($)");
        apples.stream().forEach(calAndPrinPrice);
    }

    /**
     *
     * @param a
     * @param price
     */
    private void printPrice(Apple a, Double price) {
        double total = a.getWeight() * price;
        String str = String.format("%s\t\t%10.5f", price, total);
        System.out.println(str);
    }

    /**
     * 打印Apple信息
     * @param a apple
     */
    private void print(Apple a) {
        if (a == null) {
            return;
        }
        System.out.println("name=" + a.getName());
        System.out.println("weight=" + a.getWeight());
        System.out.println("color="+a.getColor());
    }

    @Test
    @DisplayName("Long/Int/DoubleConsumer演示")
    public void test02() {
        DoubleConsumer createApple = (double x) -> {

        };

    }
}
